﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.StmpConfigManager.ViewModels;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.StmpConfigManager
{

    public class StmpConfigManagerController : BaseController
    {
        private readonly StmpConfigTask _stmpconfigTask = new StmpConfigTask();

        public ActionResult List(int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _stmpconfigTask.GetPagedList(pageIndex, pageSize);



            var model = new PagedList<StmpConfig>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {


            var model = new SaveStmpConfig();

            if (id != null)
            {
                var item = _stmpconfigTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<StmpConfig, SaveStmpConfig>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveStmpConfig savemodel)
        {

            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new StmpConfig
                                    {
                                        StmpName = savemodel.StmpName,
                                        StmpMoney = savemodel.StmpMoney,
                                        CreateDate = DateTime.Now,
                                    };
                    _stmpconfigTask.Add(model);
                }
                else
                {
                    var model = _stmpconfigTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.StmpName = savemodel.StmpName;
                    model.StmpMoney = savemodel.StmpMoney;


                    _stmpconfigTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _stmpconfigTask.Delete(id);
        }

        #endregion
    }
}